package com.tarena.lbs.message.web.service.impl;

import com.tarena.lbs.message.web.mapper.DynamicMsgFieldsMapper;
import com.tarena.lbs.message.web.service.DynamicMsgFieldsService;
import com.tarena.lbs.pojo.message.dos.DynamicMsgFields;
import com.tarena.lbs.pojo.message.dto.DynamicMsgFieldsDTO;
import com.tarena.lbs.pojo.message.param.DynamicMsgFieldsParam;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 动态消息字段服务实现类
 */
@Service
public class DymaicMsgFieldsServiceImpl implements DynamicMsgFieldsService {

    @Autowired
    DynamicMsgFieldsMapper dynamicMsgFieldsMapper;

    /**
     * 查询所有动态字段，如${用户名},
     * 动态字段会被替换成具体的数据
     * @return
     */
    @Override
    public List<DynamicMsgFieldsDTO> selectAll() {
        ArrayList<DynamicMsgFieldsDTO> dtoList = new ArrayList<>();

        //从数据库中查询所有动态字段
        List<DynamicMsgFields> list = dynamicMsgFieldsMapper.selectAll();
        for (DynamicMsgFields dynamicMsgFields :list){
            //将数据库的实体对象转换成DTO
            DynamicMsgFieldsDTO dynamicMsgFieldsDTO = new DynamicMsgFieldsDTO();
            BeanUtils.copyProperties(dynamicMsgFields,dynamicMsgFieldsDTO);
            dtoList.add(dynamicMsgFieldsDTO);
        }
        return dtoList;
    }

    @Override
    public void insertDynamicMsgField(DynamicMsgFieldsParam param) {
        DynamicMsgFields dynamicMsgFields = new DynamicMsgFields();
        dynamicMsgFields.setCreateAt(new Date());
        dynamicMsgFields.setFieldName(param.getFieldName());
        dynamicMsgFields.setStatus(0);
        dynamicMsgFieldsMapper.insert(dynamicMsgFields);
    }
}
